﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Entity.Biz;
using Rison.BusinessFacade.Facade.Biz;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Helper;

namespace Rison.Web.view
{
    public partial class InfoListViewByPic : PageDataViewBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                CurrentPage = 0;
                BindData();               
            }
        }

        protected override void BindData()
        {
            
            // get datasource
            DataSet data = GetDataSource();
            //DataTable data = GetTestDataSource();

            //get pageddatasource 
            PagedDataSource pds = new PagedDataSource();
            pds.AllowPaging = true;
            pds.DataSource = data.Tables[0].DefaultView;
            pds.PageSize = PageSize;
            pds.CurrentPageIndex = CurrentPage;
            this.labCurrentPage.Text = (CurrentPage+1).ToString();

            //RecordCount
            RecordCount = data.Tables[0].DefaultView.Count;
            this.labRowCount.Text = RecordCount.ToString();

            //PageCount
            PageCount = pds.PageCount;
            this.labPageCount.Text = PageCount.ToString();

            // dataList Databin
            this.DataList1.DataSource = pds;
            this.DataList1.DataBind();

            //pre page
            if (pds.IsFirstPage)
                this.IPrePage.Enabled = false;
            else
                this.IPrePage.Enabled = true;                

            //next page
            if (pds.IsLastPage)
                this.INextPage.Enabled = false;
            else
                this.INextPage.Enabled = true;

            data.Dispose(); data = null;
            
        }

        protected override DataSet GetDataSource()
        {
            InfosReaderFacade facade = new InfosReaderFacade("jftp");

            DataSet dsImages = facade.ReadNewsInfos(50); facade = null;

            foreach (DataRow row in dsImages.Tables[0].Rows)
            {
                if (row["MediaFileName"].ToString().StartsWith("/"))
                    row["MediaFileName"] = GlobalParams.Instance.NewsImageBaseUrl + row["MediaFileName"].ToString();
                else
                    row["MediaFileName"] = GlobalParams.Instance.NewsImageBaseUrl + "/" + row["MediaFileName"].ToString();

            }

            return dsImages;
        }

        private DataTable GetTestDataSource()
        {
            DataTable testSource = new DataTable();
            testSource.Columns.Add("InfoId", typeof(System.String));
            testSource.Columns.Add("title", typeof(System.String));
            testSource.Columns.Add("MediaFileName", typeof(System.String));           
            

            DataRow dr = null;
            for (int i = 0; i < 30; i++)
            {
                dr = testSource.NewRow();
                dr["InfoId"] = System.Guid.NewGuid().ToString();
                if (i % 3 == 0)
                {
                    dr["title"] = "祝所有客户“五一”节日快乐!" + i.ToString();
                    dr["MediaFileName"] = PageGlobalParams.PageUrlBase2 + "/style/image/tpxw1.jpg";
                }
                else if (i % 3 == 1)
                {
                    dr["title"] = "华北电视台管理机制改革咨询项目启动" + i.ToString();
                    dr["MediaFileName"] = PageGlobalParams.PageUrlBase2 + "/style/image/tpxw2.jpg";
                }
                else
                {
                    dr["title"] = "七次会议" + i.ToString();
                    dr["MediaFileName"] = PageGlobalParams.PageUrlBase2 + "/style/image/tpxw3.jpg";
                }

                testSource.Rows.Add(dr);
            }
            return testSource;
        }

        #region"分页"
        /// <summary>
        /// 每页显示的条数
        /// </summary>
        private int PageSize
        {            
            get
            {
                return 6;
            }
        }
        /// <summary>
        /// 记录数
        /// </summary>
        private int RecordCount
        {
            get { return (ViewState["RecordCount"] == null) ? 0 : Convert.ToInt32(ViewState["RecordCount"]); }
            set { ViewState["RecordCount"] = value; }
        }

        /// <summary>
        /// 多少页
        /// </summary>
        private int PageCount
        {
            get { return (ViewState["pageCount"] == null) ? 0 : Convert.ToInt32(ViewState["pageCount"]); }
            set { ViewState["pageCount"] = value; }
        }

        /// <summary>
        /// 当前第几页
        /// </summary>
        private int CurrentPage
        {
            get { return (ViewState["CurrentPageIndex"] == null) ? 0 : Convert.ToInt32(ViewState["CurrentPageIndex"]); }
            set { ViewState["CurrentPageIndex"] = value; }
        }

        protected void btnPrev_Click(object sender, System.EventArgs e)
        {
            CurrentPage = CurrentPage  - 1;
            BindData();
        }

        protected void btnNext_Click(object sender, System.EventArgs e)
        {
            CurrentPage = CurrentPage + 1;
            BindData();
        }

        protected void btnGo_Click(object sender, System.EventArgs e)
        {
             if (StringHelper.IsNumber((this.txtGo as TextBox).Text))
            {
                currentPageIndex = Convert.ToInt32((txtGo as TextBox).Text) - 1;
                if (currentPageIndex < 0) currentPageIndex = 0;
                if (currentPageIndex > PageCount - 1) currentPageIndex = pageCount - 1;
            }
            BindData();
        }
        
        #endregion

    }
}
